package com.mall.user.controller;

import com.mall.common.constant.MessageConstant;
import com.mall.common.entity.Result;
import com.mall.user.entity.dto.UserLoginDTO;
import com.mall.user.entity.po.User;
import com.mall.user.entity.vo.UserLoginVO;
import com.mall.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Tag(name = "用户模块")
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController {
    private final UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result<User> register(@Valid @RequestBody UserLoginDTO dto){
        userService.register(dto);
        return Result.success();
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO dto){
        UserLoginVO login = userService.login(dto);
        if (login != null){
            return Result.success(login);
        }
        return Result.error(MessageConstant.LOGIN_FAILED);
    }
}
